﻿using System.Collections;

namespace IOCDemo.IocVersion2;

public class ServiceCollection : IServiceCollection
{
    private Dictionary<string, ServiceDescriptor> _descriptors = new Dictionary<string, ServiceDescriptor>();

    public IEnumerator<ServiceDescriptor> GetEnumerator()
    {
        return _descriptors.Values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return _descriptors.Values.GetEnumerator();
    }

    public void Add(ServiceDescriptor item)
    {
       _descriptors[item.Id] = item;
    }

    public void Clear()
    {
        _descriptors.Clear();
    }

    public bool Contains(ServiceDescriptor item)
    {
        return _descriptors.ContainsKey(item.Id);
    }

    public void CopyTo(ServiceDescriptor[] array, int arrayIndex)
    {
        throw new NotImplementedException();
    }

    public bool Remove(ServiceDescriptor item)
    {
        return _descriptors.Remove(item.Id);
    }

    public int Count => _descriptors.Count;
    public bool IsReadOnly => false;

    public ServiceDescriptor this[string index]
    {
        get
        {
            if (_descriptors.ContainsKey(index))
            {
                return _descriptors[index];
            }

            return null;
        }
        set => _descriptors[index] = value;
    }
}